package com.kfh.ybracelet.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.bean.GroupSettingBean;

import java.util.ArrayList;

/**
 * 删除群成员的adapter
 * Created by Aaron on 16/2/25.
 */
public class RemoveNumsAdapter extends BaseAdapter {

    private Context ctx;
    private ArrayList<GroupSettingBean.TopicEntity.UserListEntity> removeList;

    public RemoveNumsAdapter(Context ctx, ArrayList<GroupSettingBean.TopicEntity.UserListEntity> removeList) {
        this.ctx = ctx;
        this.removeList = removeList;
    }

    @Override
    public int getCount() {
        return removeList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_choose_nums, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            viewHolder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
            viewHolder.tvLetter = (TextView) convertView.findViewById(R.id.catalog);
            viewHolder.tv_nickname = (TextView) convertView.findViewById(R.id.tv_nickname);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (removeList == null || removeList.get(position) == null || removeList.get(position).user == null) {
            return convertView;
        }

        viewHolder.tv_nickname.setText(removeList.get(position).user.nickName);

        Glide.with(ctx)
                .load(removeList.get(position).user.imgUrl)
                .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                .placeholder(R.mipmap.temp_pic).into(viewHolder.iv_pic);

        if (removeList.get(position).isSelect) {
            viewHolder.iv_choose.setSelected(true);
        } else {
            viewHolder.iv_choose.setSelected(false);
        }

        //根据position获取分类的首字母的char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置,则认为是第一次出现
        if (position == getPositionForSection(section)) {
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(removeList.get(position).user.sortLetters);
        } else {
            viewHolder.tvLetter.setVisibility(View.GONE);
        }

        return convertView;
    }


    /**
     * 根据Listview的当前位置获取分类的首字母的char ascii值
     */
    public int getSectionForPosition(int position) {
        return removeList.get(position).user.sortLetters.charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = removeList.get(i).user.sortLetters;
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }


    private class ViewHolder {
        private ImageView iv_choose;
        private ImageView iv_pic;
        private TextView tv_nickname;
        private TextView tvLetter;
    }

}
